import  {RequestMethod}  from "./libs/RequestMethod"
import natvieNetwork from "./libs/network"
import { NativeResult } from "./libs/types"
import {
		ref
	} from 'vue'

const TAG = 'hxjz-sdk：'
const verison = '0.0.1'


class HXJZSDK {
	TAG = TAG
	version = verison
	constructor() {
		const info = uni.getSystemInfoSync()
		console.log(TAG, info)
	}

	canIUse(apiName : string) {
		return true
	}

	isInHXApp() {
		// #ifdef H5
		return false
		// #endif 
		// #ifndef H5
		return true
		// #endif 
	}

	getSDKVersion() {
		return this.version
	}

	getAppVersion() {
		return 'appversion'
	}

	add = (a : number, b : number) => a + b
	/**
	 * promise成功的数据结构 
	 * { code: number, responseBody: AnyObject, headers: Array<[string, string]>}
	 */
	request({url , data , header ,method}: UniNamespace.RequestOptions) : Promise<AnyObject> {
		return new Promise((resolve, reject) => {
			if (url && url.startsWith("http")) {
				uni.request({
					url: url,
					method: method,
					data: data,
					header: header,
					success(data : any) {
						console.log("nativeRequest uni.request：", data)
						resolve(data)
					},
					fail(err) {
						reject(err)
					}
				})
			} else {
				natvieNetwork?.request({
					url:url, data:data, header:header,
				}, (data : any) => {
						console.log("nativeRequest natvieNetwork：", data)
					const xData = JSON.parse(data)
					if (xData.err) {
						reject(xData.err)
					} else {
						resolve(xData.data as any)
					}
				})
			}

		})
	}
	
	
	/**
	 * 打开原生页面
	 * @param {string} pageUrl 页面路径，原生：hx://{path},path(原生页面路径统一规范);小程序：appid://{appid},appid小程序appid;其他apk应用：apk://{package},package其他应用app包名
	 * @param {Object} 跳转页面携带参数 （示例：{phoneNo: '13400000062'}）
	 * @returns {Promise<void>}
	 * @example
	 * HXJZSDK.navigateTo('hx://home', { userId: 123 })
	 *   .then(() => console.log('页面打开成功'))
	 *   .catch(err => console.error('打开失败', err));
	 */
	
	navigateTo(pageUrl : string, params : AnyObject = {}) {
		return new Promise((resolve, reject) => {
			// 调用原生方法
			natvieNetwork.navigateTo({
				pageUrl: pageUrl,
				params: params
			}, (data:any) => {
				const xData = JSON.parse(data)
				if (xData.err) {
					reject(xData.err)
				} else {
					resolve(xData.data as any)
				}
			});

		});
	}
	
	
	/**
	 * Toast提示
	 * @returns void
	 * @example
	 * HXJZSDK.showToast("提示")
	 */
	showToast(msg) {
		natvieNetwork.showToast(msg)
	}
	
	/**
	 * 获取token
	 * @returns string
	 * @example
	 * HXJZSDK.getToken()
	 */
	getToken() {
		return natvieNetwork.getToken()
	}
	
	/**
	 * 获取用户信息
	 * @returns string
	 * @example
	 * HXJZSDK.getUserInfo(token)
	 */
	getUserInfo() {
		return new Promise((resolve, reject) => {
			console.log('Clicked getUserInfo');
			natvieNetwork.getUserInfo((info) => {
				if (info) {
					resolve(info);
				} else {
					reject('无法获取用户信息');
				}
			})
		});
	}

}

const hx = new HXJZSDK()

export default hx 